package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.Order;
import com.wm.bean.OrderMenu;
import com.wm.bean.OrderResMenu;
import com.wm.bean.Orders;
import com.wm.comm.Constant;
import com.wm.dao.OrderMenuDAO;
import com.wm.dao.OrderResMenuDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class OrderResMenuHibernateDAO extends HibernateDaoSupport implements OrderResMenuDAO {
	
	private static final String LOAD_BY_ID = "from OrderResMenu where id = ?";
	private static final String  REMOVE_BY_ID = "delete OrderResMenu Admin where id = ?";
	private static final String  REMOVE_BY_ORDER_ID = "delete OrderResMenu Admin where order_id = ?";

	/**
	 * 保存OrderResMenu对象
	 * 
	 * @param orderResMenu
	 * 			OrderResMenu
	 * @return OrderResMenu
	 * 			OrderResMenu
	 */
	public OrderResMenu saveOrderResMenu(OrderResMenu orderResMenu){
		this.getHibernateTemplate().save(orderResMenu);
		return orderResMenu;
	}
	
	/**
	 * 根据ID删除OrderResMenu对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeOrderResMenuById(final long id){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	/**
	 * 根据orderId删除OrderResMenu对象
	 * 
	 * @param orderId
	 * 			long
	 */
	public void removeOrderResMenuByOrderId(final long orderId){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ORDER_ID);
	          query.setLong(0, orderId);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	/**
	 * 根据ID获取OrderResMenu对象
	 * 
	 * @param id
	 * 			long
	 * @return OrderResMenu
	 * 			OrderResMenu
	 */
	public OrderResMenu findOrderResMenuById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (OrderResMenu) l.get(0);
		}
	}
	

	/**
	 * 获取OrderResMenu对象列表
	 * @param orderId
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 * 			List
	 */
	public List findOrderResMenuByOrderId(long orderId,String orderby,int ascOrDesc){
		StringBuffer sb = new StringBuffer();
		sb.append("from OrderResMenu where 1=1");
		if (orderId != -1) {
			sb.append(" and orderId=");
			sb.append(orderId);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				List list = query.list();
				return list;
			}
		});
	}
}
